#include "Widget.h"
#include "../common/utils/FileChecker.h"

void Widget::initParcel(int posX, int posY)
{
	parcel = new Parcel();
    this->getPosition(parcel);
    parcel->posX = posX;
    parcel->posY = posY;


    if(this->surface !=NULL){
    	parcel->height = this->surface->h;
    	parcel->width = this->surface->w;
    }
}

Widget::Widget(int posX, int posY, SDL_Surface* surface, Widget *parent, bool drawBorders,Color borderColor) {
	this->parcel = NULL;
	this->parent = parent;
	this->surface = surface;
	this->drawBorders = drawBorders;
	this->borderColor = borderColor;
    initParcel(posX, posY);
}

Widget::Widget(Color color, int w,int h,int posX, int posY, Widget *parent, bool drawBorders){
	this->parcel = NULL;
	this->parent = parent;
	this->surface = SurfaceUtils::colorSurface(w,h,color);
	this->drawBorders = drawBorders;
	this->borderColor = Color::AZUL;
	initParcel(posX, posY);
}

Widget::Widget(int posX, int posY, std::string imageSurface, Widget *parent, bool drawBorders) {
	this->parcel = NULL;
	this->parent = parent;
	this->surface = SurfaceUtils::loadImage(imageSurface);
	this->drawBorders = drawBorders;
	this->borderColor = Color::AZUL;
	initParcel(posX,posY);
}

void Widget::getPosition(Parcel* position) {
	position->posX = parcel->posX;
	position->posY = parcel->posY;
	if (parent != NULL) {
		Parcel* parentPosition = new Parcel();
		parent->getPosition(parentPosition);
		position->posX += parentPosition->posX;
		position->posY += parentPosition->posY;
		delete parentPosition;
	}

}

Widget::~Widget() {
	if (surface != NULL) {
		SDL_FreeSurface(surface);
		surface = NULL;
	}
	delete parcel;
}

void Widget::handleEvent(Event event){}

void Widget::resolve(){}

void Widget::showOn(SDL_Surface *canvasSurface){

	Uint32 vLineColor = borderColor.getColor();

	if(this->surface){
		if(this->drawBorders){
			vlineColor(this->surface, 0, 0, this->parcel->height, vLineColor);
			vlineColor(this->surface, this->parcel->width-1, 0, this->parcel->height, vLineColor);
			hlineColor(this->surface, 0, this->parcel->width, 0, Color::AZUL);
			hlineColor(this->surface, 0, this->parcel->width, this->parcel->height-1, vLineColor);
		}
		SDL_BlitSurface(this->surface, NULL, canvasSurface, this->parcel->getRectangle());
	}else{
		if(this->drawBorders){
			vlineColor(canvasSurface, this->parcel->posX, this->parcel->posY, this->parcel->height, vLineColor);
			vlineColor(canvasSurface, this->parcel->posX + this->parcel->width-1, this->parcel->posY, this->parcel->height, vLineColor);
			hlineColor(canvasSurface, this->parcel->posX, this->parcel->width, this->parcel->posY, vLineColor);
			hlineColor(canvasSurface, this->parcel->posX, this->parcel->width, this->parcel->posY + this->parcel->height-1, vLineColor);
		}
	}
}

bool Widget::isMouseOver(Event event) const {
	return ((event.getXCoordinate() > this->parcel->posX)
			&& (event.getXCoordinate() < this->parcel->posX + this->parcel->width)
			&& (event.getYCoordinate() > this->parcel->posY)
			&& (event.getYCoordinate() < this->parcel->posY
					+ this->parcel->height));
}

Parcel* Widget::getBoundaries(){
	return SurfaceUtils::getBoundaries(this->surface);
}
